<template>
    <div>
        <table>
            <tr>
                <td>食谱标题<span style="color: red;">*</span></td>
                <td><input type="text" v-model="data.name"></td>
            </tr>
            <tr>
                <td>分类<span style="color: red;">*</span></td>
                <td>
                    <select v-model="data.fent">
                        <option value="">请选择</option>
                        <option value="早餐">早餐</option>
                        <option value="午餐">午餐</option>
                        <option value="晚餐">晚餐</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>标签<span style="color: red;">*</span></td>
                <td><input type="text" v-model="data.benr"></td>
            </tr>
            <tr>
                <td>图片</td>
                <td>
                    <input type="file" @change="Tent">
                    <img :src="'https://localhost:7172/'+data.img" alt="" style="width: 80px;height: 80px;">
                </td>
            </tr>
            <tr>
                <td>食谱详情<span style="color: red;">*</span></td>
                <td><input type="text" v-model="data.ment"></td>
            </tr>
            <tr>
                <td><input type="button" value="添加" @click="Add"></td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref} from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';

const rount=useRouter()


const Tent=(e:any)=>{
    var f=e.target.files[0];
    var fd=new FormData();
    fd.append("file",f);
    axios.post("https://localhost:7172/api/UpLog/UpFile",fd).then(res=>{
        data.value.img=res.data;
    })
}

const data=ref({
  "id": 0,
  "name": "",
  "fent": "",
  "benr": "",
  "img": "",
  "ment": ""
})


const Add=()=>{
    if(data.value.name==""){
        alert("食谱标题不能为空")
        return;
    }
    if(data.value.fent==""){
        alert("分类不能为空")
        return;
    }
    if(data.value.benr==""){
        alert("标签不能为空")
        return;
    }
    if(data.value.ment==""){
        alert("食谱详情不能为空")
        return;
    }
    axios.post("https://localhost:7172/api/User/Add",data.value).then(res=>{
        if(res.data>0){
            alert('成功')
            rount.push({path:'/ShowView'});
        }
        else{
            alert('失败')
        }
    })
}


</script>

<style scoped>

</style>